import os
import re

os.system("svn info .. > svn-info.txt")

revision = ""
revision_pattern = re.compile(r"Revision:\s*(\d+)\D*")
revision_found = False

version_line_pattern = re.compile(r".*Assembly.*Version\D*\d+.\d+.(\d+).(\d+).*")


info_f = open("svn-info.txt", "r", 1)
for line in info_f:
    matched = revision_pattern.match(line)
    if matched:
        revision = matched.group(1)
        revision_found = True
        print revision
        break
info_f.close()

if not revision_found:
    print "Could not obtain SVN revison info."
    exit(-1)


etalon_f = open("properties/AssemblyInfo.cs", "r", 1)
patched_f = open("properties/AssemblyInfoVer.cs", "w")
for line in etalon_f:
    matched = version_line_pattern.match(line)
    if matched:
       patched_line = line[:matched.start(1)] + revision + line[matched.end(1):]
       patched_f.write(patched_line)
    else:
       patched_f.write(line)
patched_f.close()
etalon_f.close()
    
exit(0)

